import random

from ronglian_sms_sdk import SmsSDK
from flask import current_app
import redis,json
from utils.myredis import r


class SmsTool():
    def send(self, mobile, code):
        accId = current_app.config.get("ACCOUNT_SID")
        accToken = current_app.config.get("AUTH_TOKEN")
        appId = current_app.config.get("APPID")
        tid = current_app.config.get("TID")
        sdk = SmsSDK(accId, accToken, appId)

        # host = current_app.config.get("REDIS_HOST")
        # port = current_app.config.get("REDIS_PORT")
        # _db = current_app.config.get("REDIS_DB")
        # red = redis.Redis(host=host, port=port, db=_db)

        t = current_app.config.get("SMS_TIME")  # 5分钟过期

        # 把验证码存入redis中
        key = 'sms_' + mobile
        # red.set(key, code, ex=t * 60)
        r.set_str(key, code, t*60)
        # 发送验证码
        datas = (code, t)

        rs = sdk.sendMessage(tid, mobile, datas)
        print(rs,"???????????????????")
        re_data = json.loads(rs)
        print(re_data)

        if re_data["statusCode"] == "000000":
            return True
        return False


if __name__ == '__main__':
    # 开启上下文环境
    from app import app
    with app.app_context():
        s = SmsTool()
        rs = s.send("18625913631", "我爱你")
        print(rs)



